<?php
/**
 * 系统角色表模型
 * 
 * @author:  will.lee <im.will.lee@gmail.com>
 * @version: 1.0
 */
class RoleModel extends AppModel
{
    // 定义关联模型
    protected $_link = array
    (
        'RoleNode'      => array
        (
            'mapping_type'  => HAS_MANY,
            'class_name'    => 'RoleNode',
            'foreign_key'   => 'role_id',
            'mapping_name'  => 'node_items',
            'mapping_fields'=> array('role_id','node_id','g','m','a'),
        )
    );
    
    protected $_validate_list = array
    (
        // 添加时验证
        'save' => array
        (
            array('name','require','角色名称必须填写!',1),
            array('role_type',array(1,2),'角色区分不正确!',1,'in'),
            array('name','','角色名称已存在!',1,'unique'),
        )
    );
    
    
    /**
     * 获取系统角色数组
     */
    public function getRoleList($list = false)
    {
        $roles = $this->field(true)->select();
        if($list)
        {
            $return = array();
            foreach($roles as $role)
            {
                $return[$role['id']] = $role['name'];
            }
            return $return;
        }
        return $roles;
    }
    
    /**
     * 角色关联节点取得
     */
    public function getRoleNodes($role_id)
    {
        $where  = array('role_id' => $role_id);
        $list   = $this->relation(true)->field('id,name')->where($where)->select();
        return $list;
    }
}
